package cn.ibizlab.odoo.core.odoo_account.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iaccount_account;
import cn.ibizlab.odoo.core.odoo_account.domain.Account_account;
import cn.ibizlab.odoo.core.odoo_account.filter.Account_accountSearchContext;
import cn.ibizlab.odoo.core.odoo_account.service.IAccount_accountService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[科目] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Account_accountServiceImpl implements IAccount_accountService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iaccount_accountClientService account_accountClientService;

    @Override
    public Account_account get(Integer id) {
        Iaccount_account clientModel = account_accountClientService.createModel();
        clientModel.setId(id);
        account_accountClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Account_account get(Account_account account_account) {
        Account_account retaccount_account = this.get(account_account.getId()) ;
    	BeanCopier copier=BeanCopier.create(retaccount_account.getClass(), account_account.getClass(), false);
        copier.copy(retaccount_account, account_account, null);
        return account_account ;
    }

    @Override
    public boolean update(Account_account account_account) {
        boolean bOk = false ;
        Iaccount_account clientModel = this.convert2Model(account_account,null);
        account_accountClientService.update(clientModel) ;
        this.convert2Domain(clientModel,account_account) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iaccount_account clientModel = account_accountClientService.createModel();
        clientModel.setId(id);
        account_accountClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Account_account> account_accounts, int batchSize) {

        //account_accountClientService.updateBatch(account_accounts,batchSize) ;

    }

    @Override
    public boolean create(Account_account account_account) {
        boolean bOk = false ;
        Iaccount_account clientModel = this.convert2Model(account_account,null);
        account_accountClientService.create(clientModel) ;
        this.convert2Domain(clientModel,account_account);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void createBatch(List<Account_account> account_accounts, int batchSize) {

                        //account_accountClientService.createBatch(account_accounts,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //account_accountClientService.removeBatch(account_accounts) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Account_account> searchDefault(Account_accountSearchContext context) {
        List<Account_account> list = new ArrayList<Account_account>();
        Page<cn.ibizlab.odoo.core.client.model.Iaccount_account> clientModelList = account_accountClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iaccount_account clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Account_account>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据科目类型查询
     */
    @Override
    public Page<Account_account> selectByOdoo_user_type(Integer id){
        Account_accountSearchContext context = new Account_accountSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Account_account> list = new ArrayList<Account_account>();
        Page<cn.ibizlab.odoo.core.client.model.Iaccount_account> clientModelList = account_accountClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iaccount_account clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Account_account>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据科目组查询
     */
    @Override
    public Page<Account_account> selectByOdoo_group(Integer id){
        Account_accountSearchContext context = new Account_accountSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Account_account> list = new ArrayList<Account_account>();
        Page<cn.ibizlab.odoo.core.client.model.Iaccount_account> clientModelList = account_accountClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iaccount_account clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Account_account>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据公司查询
     */
    @Override
    public Page<Account_account> selectByOdoo_company(Integer id){
        Account_accountSearchContext context = new Account_accountSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Account_account> list = new ArrayList<Account_account>();
        Page<cn.ibizlab.odoo.core.client.model.Iaccount_account> clientModelList = account_accountClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iaccount_account clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Account_account>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据币种查询
     */
    @Override
    public Page<Account_account> selectByOdoo_currency(Integer id){
        Account_accountSearchContext context = new Account_accountSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Account_account> list = new ArrayList<Account_account>();
        Page<cn.ibizlab.odoo.core.client.model.Iaccount_account> clientModelList = account_accountClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iaccount_account clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Account_account>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Account_account> selectByOdoo_create(Integer id){
        Account_accountSearchContext context = new Account_accountSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Account_account> list = new ArrayList<Account_account>();
        Page<cn.ibizlab.odoo.core.client.model.Iaccount_account> clientModelList = account_accountClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iaccount_account clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Account_account>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Account_account> selectByOdoo_write(Integer id){
        Account_accountSearchContext context = new Account_accountSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Account_account> list = new ArrayList<Account_account>();
        Page<cn.ibizlab.odoo.core.client.model.Iaccount_account> clientModelList = account_accountClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iaccount_account clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Account_account>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iaccount_account convert2Model(Account_account domain , Iaccount_account model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = account_accountClientService.createModel() ;
        }

         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getOpening_debitDirtyFlag())
            model.setOpening_debit(domain.getOpening_debit());
         if(domain.getNoteDirtyFlag())
            model.setNote(domain.getNote());
         if(domain.getOpening_creditDirtyFlag())
            model.setOpening_credit(domain.getOpening_credit());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getCodeDirtyFlag())
            model.setCode(domain.getCode());
         if(domain.getTax_idsDirtyFlag())
            model.setTax_ids(domain.getTax_ids());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getDeprecatedDirtyFlag())
            model.setDeprecated(domain.getDeprecated());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getLast_time_entries_checkedDirtyFlag())
            model.setLast_time_entries_checked(domain.getLast_time_entries_checked());
         if(domain.getReconcileDirtyFlag())
            model.setReconcile(domain.getReconcile());
         if(domain.getTag_idsDirtyFlag())
            model.setTag_ids(domain.getTag_ids());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getInternal_typeDirtyFlag())
            model.setInternal_type(domain.getInternal_type());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getCurrency_id_textDirtyFlag())
            model.setCurrency_id_text(domain.getCurrency_id_text());
         if(domain.getInternal_groupDirtyFlag())
            model.setInternal_group(domain.getInternal_group());
         if(domain.getGroup_id_textDirtyFlag())
            model.setGroup_id_text(domain.getGroup_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getUser_type_id_textDirtyFlag())
            model.setUser_type_id_text(domain.getUser_type_id_text());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getGroup_idDirtyFlag())
            model.setGroup_id(domain.getGroup_id());
         if(domain.getUser_type_idDirtyFlag())
            model.setUser_type_id(domain.getUser_type_id());
         if(domain.getCurrency_idDirtyFlag())
            model.setCurrency_id(domain.getCurrency_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
        return model ;
    }

    public Account_account convert2Domain( Iaccount_account model ,Account_account domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Account_account();
        }

        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getOpening_debitDirtyFlag())
            domain.setOpening_debit(model.getOpening_debit());
        if(model.getNoteDirtyFlag())
            domain.setNote(model.getNote());
        if(model.getOpening_creditDirtyFlag())
            domain.setOpening_credit(model.getOpening_credit());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getCodeDirtyFlag())
            domain.setCode(model.getCode());
        if(model.getTax_idsDirtyFlag())
            domain.setTax_ids(model.getTax_ids());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getDeprecatedDirtyFlag())
            domain.setDeprecated(model.getDeprecated());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getLast_time_entries_checkedDirtyFlag())
            domain.setLast_time_entries_checked(model.getLast_time_entries_checked());
        if(model.getReconcileDirtyFlag())
            domain.setReconcile(model.getReconcile());
        if(model.getTag_idsDirtyFlag())
            domain.setTag_ids(model.getTag_ids());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getInternal_typeDirtyFlag())
            domain.setInternal_type(model.getInternal_type());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getCurrency_id_textDirtyFlag())
            domain.setCurrency_id_text(model.getCurrency_id_text());
        if(model.getInternal_groupDirtyFlag())
            domain.setInternal_group(model.getInternal_group());
        if(model.getGroup_id_textDirtyFlag())
            domain.setGroup_id_text(model.getGroup_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getUser_type_id_textDirtyFlag())
            domain.setUser_type_id_text(model.getUser_type_id_text());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getGroup_idDirtyFlag())
            domain.setGroup_id(model.getGroup_id());
        if(model.getUser_type_idDirtyFlag())
            domain.setUser_type_id(model.getUser_type_id());
        if(model.getCurrency_idDirtyFlag())
            domain.setCurrency_id(model.getCurrency_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        return domain ;
    }


}
